<template>
    <el-dialog title="特殊理箱结算" :visible.sync="dialogForm" width="1100px">
        <el-table :data="list" border stripe max-height="500" class="mt-10">
            <el-table-column prop="containerId" label="箱号"  min-width="120"></el-table-column>
            <el-table-column prop="containerSize" label="尺寸"  min-width="75"></el-table-column>
            <el-table-column prop="containerType" label="箱型"  min-width="75"></el-table-column>
            <el-table-column prop="ef" label="空重"  min-width="100">
                <template slot-scope="scope">
                    {{ scope.row.ef | _filterIfHeavyType }}
                </template>
            </el-table-column>
            <el-table-column prop="planType" label="作业类型"  min-width="120">
                <template slot-scope="scope">
                    {{ scope.row.planType | _filterTsJobStatus }}
                </template>
            </el-table-column>
            <el-table-column prop="sealNo" label="铅封号"  min-width="120"></el-table-column>
            <el-table-column prop="finishTime" label="施封日期"  min-width="160"></el-table-column>
            <el-table-column prop="shipAgentName" label="船代"  min-width="120"></el-table-column>
            <el-table-column fixed="right" align="center" label="操作"  width="140">
                <template slot-scope="scope">
                    <el-button size="small" type="text" @click="handleRemove(scope.$index)">移除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-form :inline="true" :model="form" ref="form" class="search-form" :rules="rules" label-width="100px">
            <el-row>
                <el-col :span="8">
                    <el-form-item label="合计：" prop="containerId">
                        20*F {{ tf }} --- 40*F {{ ff }}
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="结算方式：" prop="accountWay">
                        <el-select v-model="form.accountWay" placeholder="请选择" clearable>
                            <el-option v-for="[key, val] in _dictSettlementType" :key="key" :label="val" :value="key">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="结算单位：" prop="accountUnit">
                        <auto-search-custom @selectName="handleAccountUnit"
                                            :name="form.accountUnit" :isShipAgent="true"></auto-search-custom>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row v-show="form.accountWay==='C'">
                <el-col :span="8">
                    <el-form-item label="付费金额：">
                        {{ form.accountFee }}
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogForm = false">取 消</el-button>
            <el-button type="primary" @click="handleSubmit" :loading="$store.getters.loadingBtn">保 存</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import autoSearchCustom from '@/components/autocomplete/autoSearchCustom'
    import { getSpecialNum, postFeeConfirm } from "@/api/shipPlan/shipPlan";

    export default {
        name: "specialSettleForm",
        components: { autoSearchCustom },
        data() {
            return {
                dialogForm: false,
                list: [],
                form: this.clearForm(),
                rules: {},
            }
        },
        mounted() {

        },
        methods: {
            loadData() {
                let list = []
                for (let i = 0; i < this.list.length; i++) {
                    list.push({ id: this.list[i].planId,accountWay:this.form.accountWay,accountUnit:this.form.accountUnit })
                }
                getSpecialNum(list).then(res => {
                    this.form.accountFee = res.data
                })
            },
            handleRemove(index) {
                this.list.splice(index, 1)
                if (this.list.length > 0) {
                    this.loadData()
                } else {
                    this.form.accountFee = 0
                }
            },
            handleSubmit() {
                let devanTypes = []
                for (let i = 0; i < this.list.length; i++) {
                    devanTypes.push({
                        id: this.list[i].planId,
                    })
                }
                let data = {
                    accountUnit: this.form.accountUnit,
                    accountWay: this.form.accountWay,
                    devanTypes:devanTypes,
                }
                if(data.accountWay==='C'){
                    data.accountFee=this.form.accountFee
                }
                postFeeConfirm(data).then(res => {
                    this.$message({ message: '结算成功!', type: 'success' })
                    this.dialogForm = false
                })
            },
            handleAccountUnit(val) {
                this.form.accountUnit=val
                this.loadData()
            },
            clearForm() {
                return {
                    accountWay: 'C',
                    accountUnit: '',
                    accountFee: 0,
                }
            }
        },
        computed: {
            // 20*F
            tf() {
                let tf = this.list.filter(f => {
                    return f.ef === 'F' && f.containerSize === '20'
                })
                return tf.length
            },
            // 40*F
            ff() {
                let ff = this.list.filter(f => {
                    return f.ef === 'F' && f.containerSize === '40'
                })
                return ff.length
            },
        },
        watch: {
            dialogForm(bool) {
                if (!bool) {
                    this.$emit('load')
                }
            }
        }
    }
</script>

<style scoped>

</style>
